//
//  QuestionsViewController.m
//  SafeFoodManagerDemo
//
//  Created by ios-1 on 2018/3/7.
//  Copyright © 2018年 bob. All rights reserved.
//

#import "QuestionsViewController.h"
#import "PatrolTableViewCell.h"
#import "SubjectListTableViewCell.h"
#import "ExaminationModel.h"
#import "QuestionsModel.h"
#import "SendPatrolViewController.h"
#import "AddRecruitViewController.h"
#import "ExaminationQuestionViewController.h"
@interface QuestionsViewController ()
@property(nonatomic,copy)NSString*SearchKeyWord;
@end

@implementation QuestionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择试卷";
    self.homeTableView.estimatedRowHeight=70;
    
    
    [self.homeTableView registerNib:[UINib nibWithNibName:@"SubjectListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.homeTableView.tableHeaderView = [self tabHeadView];
    [self.homeTableView.mj_header beginRefreshing];
}

- (UIView*)tabHeadView
{
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    headview.backgroundColor = [UIColor clearColor];
    headview.backgroundColor = [UIColor redColor];
    return headview;
}
#pragma mark-tablevie代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExaminationData *data=self.dataArr[indexPath.row];
    
    SubjectListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.SubjectNameLabel.text = data.name;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExaminationData *data=self.dataArr[indexPath.row];

    [self questionPaperHttp:data.ID Data:data];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

#pragma mark-跳转事件
-(void)PushChangeStatueVC
{
    BOOL isSelf=self.Reqmodel.shopId==0?YES:NO;
    
    BaseSearchChooseViewController *Choose=[[BaseSearchChooseViewController alloc]init];
    
    Choose.ReqStr=[SearchAndChooseManager getChooseUrlWithType:PatrolListType];
    
    if (isSelf) {
        
        if ([self.Reqmodel.ptype isEqualToString:@"1"]) {
            
            Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:PatrolListType andIsLookSelf:isSelf];
            
        }else if ([self.Reqmodel.ptype isEqualToString:@"2"])
        {
            
            NSArray *PostArr= [USER_DEFAULT objectForKey:@"TBChoose"];//如果本地存在则读取本地的
            
            if (PostArr.count!=0) {
                
                Choose.CustomArr=PostArr;
                
            }else{
                
                Choose.CustomArr=@[@{@"fieldName":@"marketIds",@"name":@"开办方",@"value":@""},@{@"fieldName":@"type",@"name":@"",@"value":@""},@{@"fieldName":@"checkUserIds",@"name":@"检查员",@"value":@""},@{@"fieldName":@"startDate",@"name":@"开始日期",@"value":@""},@{@"fieldName":@"endDate",@"name":@"结束日期",@"value":@""}];
            }
        }
        
    }else{
        
        Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:PatrolListType andIsLookSelf:isSelf];
    }
    Choose.SearchPlace=self.searchName;
    
    Choose.KeyWord=self.SearchKeyWord;
    
    Choose.Lshopid=self.Reqmodel.shopId;
    
    Choose.SureBlock=^(NSString *paras,NSString*keyword)
    {
        self.Reqmodel.params=paras;
        
        self.SearchKeyWord=keyword;
        
        [self.homeTableView.mj_header beginRefreshing];
    };
    
    [self.navigationController pushViewController:Choose animated:YES];
}

#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
    
}


/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{
    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}


-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/examList.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        ExaminationModel *InforModel=[ExaminationModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
            if (InforModel.data.count==0&&self.Reqmodel.currentPage==1) {
                
                [self.dataArr removeAllObjects];
                
                
                self.Type=TBNODateType;
                
                if (self.deleStatue) {
                    //如果处于删除状态
                    [self GotoEdit:self.btn];
                }
                
                self.btn.hidden=YES;
                
                [self.homeTableView reloadData];
                
            }else if (InforModel.data.count==0&&self.Reqmodel.currentPage!=1){
                
                self.btn.hidden=NO;
                [self.homeTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else{
                
                self.btn.hidden=NO;
                
                if (self.Reqmodel.currentPage==1) {
                    
                    [self.dataArr removeAllObjects];
                    
                }
                
                [self.dataArr addObjectsFromArray:InforModel.data];
                
                [self.homeTableView reloadData];
                
            }
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
}
//创建考卷
- (void)questionPaperHttp:(NSString*)examId Data:(ExaminationData*)data
{
    NSString* staffId=[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:staffId forKey:@"staffId"];
    [params setObject:examId forKey:@"examId"];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/paperAdd.json" andrequestTime:5 andParams:params andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        if (responseStatus==1) {
            //请求成功
            ExaminationQuestionViewController *Patro=[ExaminationQuestionViewController new];
            JobListModel *list=[JobListModel new];
            list.pageOn = YES;
            list.pageSize = 10;
            list.currentPage = 1;
            list.paperId = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
            
            Patro.ishidenSearch = YES;
            Patro.Reqmodel = list;
            Patro.isEdit=YES;
            Patro.isdeleMore=NO;
            Patro.isGroupStyle=YES;
            Patro.AddBtnTitle= [LoginStatuesModel getUserType]==4?@"封卷":@"交卷";
            Patro.title= data.name;
            Patro.paperId = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
            [self.navigationController pushViewController:Patro animated:YES];
    
        }else if (responseStatus==0||responseStatus==2)
        {
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
